package com.bw.lib_net

import com.tencent.mmkv.MMKV
import com.zyj.retrofit.adapter.FlowCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class RetrofitManager {
    companion object {
        fun getRetrofit(): Retrofit {
            val client = OkHttpClient.Builder()
                .readTimeout(3, TimeUnit.SECONDS)
                .writeTimeout(3, TimeUnit.SECONDS)
                .connectTimeout(3, TimeUnit.SECONDS)
                .addInterceptor(interceptor = {
                    val token = MMKV.defaultMMKV().decodeString("token", "")
                    val request = it.request().newBuilder().addHeader("token", token!!).build()
                    it.proceed(request = request)
                })
                .build()

            val retrofit = Retrofit.Builder()
                .client(client)
                .baseUrl("http://10.161.9.80:7015/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(FlowCallAdapterFactory.create())
                .build()

            return retrofit
        }
    }
}